unit clsLinhaVenda;

interface

uses Dm, dbtables, SysUtils;

type
  TLinhaVenda = class(TObject)
  private
    Rep_Codigo: string;
    Linha_Codigo: string;
  public
    Linha_Descricao: string;
    function Gravar(p_Rep_Codigo, p_Linha_Codigo: string): Byte;
    function Recuperar(p_Rep_Codigo, p_Linha_Codigo: string): Byte;
    function Excluir(p_Rep_Codigo, p_Linha_Codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_rep, p_nome: string);
    function Ultimo(p_Rep_Codigo: string): string;
  end;

implementation

uses DB, biblio;

function TLinhaVenda.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM LinhaVenda where Rep_Codigo=:k_rep_Codigo and Linha_Codigo=:k_Linha_Codigo');
  tq.parambyname('k_Rep_Codigo').asstring := strzero(p_rep_codigo, 3);
  tq.parambyname('k_Linha_Codigo').asstring := strzero(p_Linha_Codigo, 3);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO LinhaVenda VALUES(:k_rep_codigo,:k_Linha_Codigo,:k_Linha_Descricao)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE LinhaVenda SET Linha_Descricao=:k_Linha_Descricao WHERE rep_codigo=:k_rep_codigo and Linha_Codigo=:k_Linha_Codigo');
  end;
  tq.parambyname('k_Rep_Codigo').asstring := strzero(p_rep_codigo, 3);
  tq.parambyname('k_Linha_Codigo').asstring := strzero(p_Linha_Codigo, 3);
  tq.parambyname('k_Linha_Descricao').asstring := Linha_Descricao;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TLinhaVenda.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM LinhaVenda where Rep_Codigo=:k_rep_codigo and Linha_Codigo=:k_Linha_Codigo');
  tq.parambyname('k_Rep_Codigo').asstring := strzero(p_rep_codigo, 3);
  tq.Parambyname('k_linha_codigo').asstring := strzero(p_Linha_Codigo, 3);
  tq.open;
  Rep_Codigo := p_Rep_Codigo;
  Linha_Codigo := p_Linha_Codigo;
  if tq.eof then begin
    Linha_Descricao := '';
    Result := 0;
  end else begin
    Linha_Descricao := tq.fieldbyname('Linha_Descricao').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TLinhaVenda.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM LinhaVenda WHERE Rep_Codigo=:k_Rep_Codigo and Linha_Codigo=:k_Linha_Codigo');
  tq.parambyname('k_rep_codigo').asstring := strzero(p_rep_codigo, 3);
  tq.parambyname('k_Linha_Codigo').asstring := strzero(p_Linha_Codigo, 3);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TLinhaVenda.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from LinhaVenda where Rep_Codigo=:k_Rep_Codigo and Linha_Descricao>=:k_Linha_Descricao order by Linha_Descricao');
  p_q.parambyname('k_rep_codigo').asstring := strzero(p_rep, 3);
  p_q.parambyname('k_Linha_Descricao').asstring := p_nome;
end;

function TLinhaVenda.Ultimo(p_Rep_Codigo: string): string;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('select max(linha_codigo) Ultimo from LinhaVenda ' +
    'where Rep_Codigo = ' + quotedstr(p_Rep_Codigo));
  tq.Open;
  if tq.FieldByName('Ultimo').IsNull then
    Result := '001'
  else
    Result := StrZero(IntToStr(tq.fieldbyname('Ultimo').AsInteger + 1), 3);
  tq.Close;
  tq.Free;
end;

end.
